package com.tencent.qcloud.tim.demo.login;


import static com.tencent.qcloud.tim.demo.TIMAppService.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.tencent.qcloud.tim.demo.R;
import com.tencent.qcloud.tim.demo.TIMAppService;
import com.tencent.qcloud.tim.demo.bean.UserInfo;
import com.tencent.qcloud.tim.demo.config.AppConfig;
import com.tencent.qcloud.tim.demo.main.MainActivity;
import com.tencent.qcloud.tim.demo.main.MainMinimalistActivity;
import com.tencent.qcloud.tim.demo.signature.GenerateTestUserSig;
import com.tencent.qcloud.tim.demo.utils.DBHelper;
import com.tencent.qcloud.tim.demo.utils.DemoLog;
import com.tencent.qcloud.tim.demo.utils.TUIUtils;
import com.tencent.qcloud.tuicore.interfaces.TUICallback;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import androidx.appcompat.app.AppCompatActivity;

public class NewUserPasswordActivity extends AppCompatActivity {

    private EditText etNewPassword, etConfirmPassword;
    private String phone;
    private EditText etPhone_register;
    private Button btnRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newuser_setpassword); // 设置密码界面的布局

        // 获取传递过来的手机号
        phone = getIntent().getStringExtra("phone");
        etPhone_register=findViewById(R.id.etPhone_register);
        etPhone_register.setText(phone);
        btnRegister=findViewById(R.id.btnRegister);
        // 初始化视图
        etNewPassword = findViewById(R.id.etCode_register);
        etConfirmPassword = findViewById(R.id.etCodeAgain);

        // 设置按钮点击监听器
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPassword();
            }
        });
    }

    private void setPassword() {
        String password = etNewPassword.getText().toString();
        String confirmPassword = etConfirmPassword.getText().toString();

        // 输入验证
        if (password.isEmpty() || confirmPassword.isEmpty()) {
            Toast.makeText(NewUserPasswordActivity.this, "请填写密码字段", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!password.equals(confirmPassword)) {
            Toast.makeText(NewUserPasswordActivity.this, "密码不匹配", Toast.LENGTH_SHORT).show();
            return;
        }

        // 设置密码逻辑（保存到数据库或其他）
        Toast.makeText(NewUserPasswordActivity.this, "密码设置成功", Toast.LENGTH_SHORT).show();
        // 此处可以保存密码，完成注册逻辑
        // 调用 DBHelper 注册方法
        new Thread(new Runnable() {
            @Override
            public void run() {
                int result = DBHelper.registerUser(phone, password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        switch (result) {
                            case 1:
                                Toast.makeText(NewUserPasswordActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                // 登录成功，跳转到主页面
                                TIMAppService.getInstance().initBeforeLogin(0);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        btnRegister.setEnabled(false);
                                    }
                                });

                                final String userID = phone;
                                final String userSig = GenerateTestUserSig.genTestUserSig(phone);

                                LoginWrapper.getInstance().loginIMSDK(NewUserPasswordActivity.this, AppConfig.DEMO_SDK_APPID, userID, userSig, TUIUtils.getLoginConfig(), new TUICallback() {
                                    @Override
                                    public void onError(final int code, final String desc) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtil.toastLongMessage(getString(R.string.failed_login_tip) + ", errCode = " + code + ", errInfo = " + desc);
                                                btnRegister.setEnabled(true);
                                            }
                                        });
                                        DemoLog.i(TAG, "imLogin errorCode = " + code + ", errorInfo = " + desc);
                                    }

                                    @Override
                                    public void onSuccess() {
                                        UserInfo.getInstance().setUserId(userID);
                                        UserInfo.getInstance().setUserSig(userSig);
                                        UserInfo.getInstance().setAutoLogin(true);
                                        UserInfo.getInstance().setDebugLogin(true);

                                        // 登录成功，跳转到主页面
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Intent intent ;
                                                if (AppConfig.DEMO_UI_STYLE == 0) {
                                                    intent = new Intent(NewUserPasswordActivity.this, MainActivity.class);
                                                } else {
                                                    intent = new Intent(NewUserPasswordActivity.this, MainMinimalistActivity.class);
                                                }

                                                startActivity(intent);
                                                TIMAppService.getInstance().registerPushManually();
                                                finish();
                                            }
                                        });
                                    }
                                });

                                break;
                            case -1:
                                Toast.makeText(NewUserPasswordActivity.this, "该手机号已注册", Toast.LENGTH_SHORT).show();
                                break;
                            case 0:
                                Toast.makeText(NewUserPasswordActivity.this, "注册失败，请稍后重试", Toast.LENGTH_SHORT).show();
                                break;
                            case -2:
                                Toast.makeText(NewUserPasswordActivity.this, "注册过程中出现错误", Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
            }
        }).start();
    }
}
